//
//  ForgetPwdVC.swift
//  ShareUmbrella
//
//  Created by keng fu on 2017/8/10.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//
//  Roger:18684909663

import UIKit
import YSKit
import TSMessages

class ForgetPwdVC: YSBaseViewController {
    
    var code = ""
    var phone = ""
    
    @IBOutlet weak var tf_newPwd: UITextField!
    
    @IBOutlet weak var tf_newPwdAg: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = I18n("VC.login.forgetpwd.title")
        tf_newPwd.changePlaceholderColor(UIColor.init(red: 65, green: 181, blue: 116))
        tf_newPwdAg.changePlaceholderColor(UIColor.init(red: 65, green: 181, blue: 116))
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func finishAction(_ sender: UIButton) {
        if !self.verifyInput() {
            return;
        }
        
        HUD.show()
        _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.forgetPwd(phone: phone, code: code, password: tf_newPwd.text!))).subscribe(onNext: { [weak self] (data) in
            //
            HUD.hide()
            guard let _ = self else {
                return
            }
            self?.navigationController?.popToRootViewController(animated: true)
            }, onError: { [weak self](ErrorType) in
                HUD.hide()
                guard let  `self` = self else {
                    return
                }
                self.handle(withError: ErrorType)
        })
    }
    
    func verifyInput() -> Bool {
        if tf_newPwd.text?.characters.count == 0, tf_newPwdAg.text?.characters.count == 0{
            TSMessage.showNotification(withTitle: I18n("VC.login.nopwd"), type: .warning)
            return false;
        }
        
        if (tf_newPwd.text?.characters.count)! < 6, (tf_newPwdAg.text?.characters.count)! < 6 {
            TSMessage.showNotification(withTitle: I18n("VC.login.pwdlength"), type: .warning)
            return false;
        }
        
        if (tf_newPwd.text?.characters.count)! > 20, (tf_newPwdAg.text?.characters.count)! > 20 {
            TSMessage.showNotification(withTitle: I18n("VC.login.pwdlengthtoolong"), type: .warning)
            return false;
        }
        
        if (tf_newPwd.text != tf_newPwdAg.text) {
            TSMessage.showNotification(withTitle: I18n("VC.login.pwdnotsame"), type: .warning)
            return false;
        }
        
        return true;
    }

    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
